package com.lib.common.widget.dialog.share

import android.content.Context
import android.graphics.Rect
import android.view.Gravity
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lib.common.R
import com.lib.common.databinding.WidgetShareDialogBinding
import com.lib.common.helper.sharehelper.AShareListener
import com.lib.common.helper.sharehelper.ShareHelper
import com.lib.common.helper.sharehelper.entity.ShareData
import com.lib.common.helper.sharehelper.entity.ShareType
import com.lib.framework.base.BaseActivity
import com.lib.framework.ext.ddp
import com.lib.framework.manager.LogManager
import com.lib.framework.widget.dialog.BaseDialog

/**
 * 分享对话框
 *
 * @author keanbin
 */
class ShareDialog(context: Context) : BaseDialog(context, R.style.defaultDialogTheme04) {

    companion object {
        private const val TAG = "ShareDialog"
    }


    private var mDataBinding: WidgetShareDialogBinding? = null


    /**
     * 分享列表的 Adapter
     */
    private var mShareListAdapter: ShareListAdapter? = null

    /**
     * 要分享的数据
     */
    private var mShareData: ShareData? = null

    init {
        initView()
        window?.let {
            it.setGravity(Gravity.BOTTOM) // 此处可以设置dialog显示的位置
            it.setWindowAnimations(R.style.BottomInAndOutStyle) // 添加动画

            // 设置 宽度充满整个屏幕
            val display = it.windowManager.defaultDisplay
            it.attributes.width = display.width //设置dialog的宽度为当前手机屏幕的宽度
        }
    }

    /**
     * 初始化视图
     */
    private fun initView() {
        // 分享初始化
        ShareHelper.init()

        mDataBinding =
            DataBindingUtil.inflate(layoutInflater, R.layout.widget_share_dialog, null, false)
        mDataBinding?.let { dataBinding ->
            setContentView(dataBinding.root)

            dataBinding.tvCancel.setOnClickListener {
                cancel()
            }

            dataBinding.rvShareList.layoutManager = GridLayoutManager(context, 5)
            dataBinding.rvShareList.addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    view: View,
                    parent: RecyclerView,
                    state: RecyclerView.State
                ) {
                    outRect.bottom = 20.ddp
                }
            })

            mShareListAdapter = ShareListAdapter(mContext, mutableListOf())

            mShareListAdapter?.mOnClickListener = object : ShareListAdapter.OnClickListener {
                override fun onClick(item: ShareItem, position: Int) {
                    val shareData = mShareData ?: return
                    when (item.type) {
                        else -> {
                            share(item, shareData)
                        }
                    }
                }
            }

            dataBinding.rvShareList.adapter = mShareListAdapter
        }
    }

    /**
     * 设置标题
     */
    fun setTitle(title: String) {
        mDataBinding?.tvTitle?.text = title
    }

    /**
     * 设置数据
     */
    fun setData(shareData: ShareData, shareItemTypeList: List<Int>) {
        mShareData = shareData

        mDataBinding?.let { dataBinding ->
            val shareItemList = mutableListOf<ShareItem>()
            for (shareItemType in shareItemTypeList) {
                when (shareItemType) {
                    ShareItem.TYPE_WEI_XIN_FRIEND_SHARE -> shareItemList.add(
                        ShareItem(shareItemType, R.mipmap.ic_weixin02, "微信好友")
                    )
                    ShareItem.TYPE_WEI_XIN_FRIEND_CIRCLE_SHARE -> shareItemList.add(
                        ShareItem(shareItemType, R.mipmap.ic_friend_circle, "朋友圈")
                    )
                    ShareItem.TYPE_QQ_SHARE -> shareItemList.add(
                        ShareItem(shareItemType, R.mipmap.ic_qq, "QQ好友")
                    )
                    ShareItem.TYPE_QQ_ZONE_SHARE -> shareItemList.add(
                        ShareItem(shareItemType, R.mipmap.ic_qq_zone, "QQ空间")
                    )
                    ShareItem.TYPE_WEI_BO_SHARE -> shareItemList.add(
                        ShareItem(shareItemType, R.mipmap.ic_weibo, "微博")
                    )
                }
            }

            mShareListAdapter?.getItems()?.clear()
            if (shareItemList.isNotEmpty()) {
                mShareListAdapter?.getItems()?.addAll(shareItemList)
            }
            mShareListAdapter?.notifyDataSetChanged()
        }
    }

    /**
     * 分享
     */
    private fun share(item: ShareItem, shareData: ShareData) {
        val context = mContext
        if (context !is BaseActivity) return

        val shareType = when (item.type) {
            ShareItem.TYPE_QQ_SHARE -> ShareType.QQ_SHARE
            ShareItem.TYPE_QQ_ZONE_SHARE -> ShareType.QQ_ZONE_SHARE
            ShareItem.TYPE_WEI_XIN_FRIEND_CIRCLE_SHARE -> ShareType.WEI_XIN_FRIEND_CIRCLE_SHARE
            ShareItem.TYPE_WEI_XIN_FRIEND_SHARE -> ShareType.WEI_XIN_FRIEND_SHARE
            ShareItem.TYPE_WEI_BO_SHARE -> ShareType.WEI_BO_SHARE
            else -> return
        }

        ShareHelper.share(context, shareType, shareData, object : AShareListener() {
            override fun onShareSuccess() {
                LogManager.i(TAG, "onShareSuccess")
            }

            override fun onShareFail(msg: String) {
                LogManager.i(TAG, "onShareFail msg = $msg")
                showPrompt(msg)
            }

            override fun onShareCancel() {
                LogManager.i(TAG, "onShareCancel")
            }
        })
        cancel()
    }

//    /**
//     * 检测 图片 是否已经下载，未下载，先下载下来
//     */
//    private fun checkImgUri(shareData: ShareData) {
//        val imgUrl = shareData.imgUri?.toString()
//        if (imgUrl?.startsWith("http") == true) {
//            var file: File? = null
//            val future = Glide.with(mContext.applicationContext)
//                .asFile()
//                .load(imgUrl)
//                .submit()
//
//            try {
//                file = future.get()
//            } catch (e: Exception) {
//                LogManager.e("ShareHelper", e)
//            }
//            file?.let {
//                shareData.imgUri = FileUtil.fileToUri(it)
//            }
//        }
//    }
}